package com.iye.puretennis.utils;

import com.iye.puretennis.domain.*;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.LinkedList;

/**
 * Created with IntelliJ IDEA.
 * User: jing wu
 * Date: 2014/12/24
 * Time: 13:19
 */
public class JsonUtils {

    public static UserInfo toUserInfo(String result) {
        try {
            JSONObject json = new JSONObject(result);
            String guid = json.getString("guid");
            String name = json.getString("nickname");
            String gender = json.getString("gender");
            String idolGuid = json.getString("idolGuid");
            String imageBase64Byte = json.getString("imageBase64Byte");
            boolean uploadImage = json.getBoolean("uploadImage");
            boolean friend = json.getBoolean("friend");
            JSONObject locationObj = json.getJSONObject("location");
            Location location = null;
            if (locationObj != null) {
                String city = locationObj.getString("city");
                String latitude = locationObj.getString("latitude");
                String longitude = locationObj.getString("longitude");
                String lastUpdate = locationObj.getString("lastupdate");
                location = new Location(city, latitude, longitude, lastUpdate);
            }
            return new UserInfo(guid, name, gender, idolGuid, imageBase64Byte, friend, location, uploadImage);
        } catch (JSONException e) {
            e.printStackTrace();
            return null;

        }
    }

    public static UserDetail toUserDetail(String result) {
        try {
            JSONObject json = new JSONObject(result);
            String guid = json.getString("guid");
            String name = json.getString("nickname");
            String imageBase64Byte = json.getString("imageBase64Byte");
            boolean uploadImage = json.getBoolean("uploadImage");
            JSONObject playYearObj = json.getJSONObject("playYear");
            String playYear = playYearObj.getString("playYear");
            int year = playYearObj.getInt("year");
            int month = playYearObj.getInt("month");
            JSONObject qianDaoObj = json.getJSONObject("qianDao");
            int accumulatedPoints = qianDaoObj.getInt("accumulatedPoints");
            int qianDaoDays = qianDaoObj.getInt("qianDaoDays");
            boolean canQianDao = !qianDaoObj.getBoolean("canQianDao");
            return new UserDetail(guid, name, imageBase64Byte, accumulatedPoints, playYear, year, month, canQianDao, qianDaoDays, uploadImage);
        } catch (JSONException e) {
            e.printStackTrace();
            return null;

        }
    }

    public static Appointment toAppointment(String result, String currentUserGuid) {
        try {

            final JSONObject jsonObj = new JSONObject(result);
            String guid = jsonObj.getString("guid");
            JSONObject ownerObj = jsonObj.getJSONObject("owner");
            String ownerName = ownerObj.getString("name");
            String ownerGuid = ownerObj.getString("guid");
            User owner = new User(ownerGuid, ownerName);
            String location = jsonObj.getString("location");
            String date = jsonObj.getString("date");
            JSONArray periodsArray = jsonObj.getJSONArray("periods");
            String startTime = periodsArray.getString(0);
            String endTime = periodsArray.getString(1);
            int participantsAmount = jsonObj.getInt("participantsAmount");
            String paymentWay = jsonObj.getString("paymentWay");
            String note = jsonObj.getString("note");
            boolean operationInvitation = jsonObj.getBoolean("canOperationInvitation");
            boolean sendJoinRequest = jsonObj.getBoolean("canSendJoinRequest");
            boolean leave = jsonObj.getBoolean("canLeave");
            JSONArray participantsArray = jsonObj.getJSONArray("participants");
            LinkedList<ListItem> participants = new LinkedList<ListItem>();
            for (int i = 0; i < participantsArray.length(); i++) {
                JSONObject participantObj = participantsArray.getJSONObject(i);
                JSONObject userObj = participantObj.getJSONObject("user");
                String userGuid = userObj.getString("guid");
                String userName = userObj.getString("name");
                ListItem listItem = new ListItem(null, guid, userName);
                listItem.setGuid3(userGuid);
                listItem.setOwner(userGuid.equals(currentUserGuid));
                participants.add(listItem);
            }
            JSONArray invitationsArray = jsonObj.getJSONArray("invitations");
            LinkedList<ListItem> invitations = new LinkedList<ListItem>();
            for (int j = 0; j < invitationsArray.length(); j++) {
                JSONObject invitationObj = invitationsArray.getJSONObject(j);
                String invitationGuid = invitationObj.getString("guid");
                JSONObject userObj = invitationObj.getJSONObject("invitee");
                String userGuid = userObj.getString("guid");
                String userName = userObj.getString("name");
                ListItem listItem = new ListItem(null, guid, userName);
                listItem.setGuid2(invitationGuid);
                listItem.setGuid3(userGuid);
                listItem.setOwner(userGuid.equals(currentUserGuid));
                invitations.add(listItem);
            }
            JSONArray requestsArray = jsonObj.getJSONArray("requests");
            LinkedList<ListItem> requests = new LinkedList<ListItem>();
            for (int k = 0; k < requestsArray.length(); k++) {
                JSONObject requestObj = requestsArray.getJSONObject(k);
                String requestGuid = requestObj.getString("guid");
                JSONObject userObj = requestObj.getJSONObject("sender");
                String userGuid = userObj.getString("guid");
                String userName = userObj.getString("name");
                ListItem listItem = new ListItem(null, guid, userName);
                listItem.setGuid2(requestGuid);
                listItem.setGuid3(userGuid);
                listItem.setOwner(userGuid.equals(currentUserGuid));
                requests.add(listItem);
            }
            return new Appointment(guid, owner, location, date, startTime, endTime, participantsAmount, paymentWay, note, invitations, participants, requests, operationInvitation, sendJoinRequest, leave);

        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static AppointmentRequest toAppointmentRequest(String result, String appointmentGuid) {
        try {
            JSONObject jsonObj = new JSONObject(result);
            String requestGuid = jsonObj.getString("guid");
            JSONObject senderObj = jsonObj.getJSONObject("sender");
            String senderGuid = senderObj.getString("guid");
            String senderName = senderObj.getString("name");
            return new AppointmentRequest(appointmentGuid, requestGuid, senderGuid, senderName);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static UserToken toUserToken(String result) {
        try {
            JSONObject jsonObj = new JSONObject(result);
            String guid = jsonObj.getString("guid");
            String token = jsonObj.getString("token");
            boolean setup = jsonObj.getBoolean("setup");

            return new UserToken(guid, token, setup, null, null);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }



    public static Profile toProfile(String result) {
        try {
            JSONObject json = new JSONObject(result);
            String guid = json.getString("guid");
            String name = json.getString("nickname");
            String gender = json.getString("gender");
            String idolGuid = json.getString("idolGuid");
            String imageBase64Byte = json.getString("imageBase64Byte");
            return new Profile(guid, name, gender, idolGuid, imageBase64Byte);
        } catch (JSONException e) {
            e.printStackTrace();
            return null;
        }
    }


}
